package icu.sehnen.user.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import icu.sehnen.common.result.CommonResult;
import icu.sehnen.user.entity.Permission;
import icu.sehnen.user.entity.User;
import icu.sehnen.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author icu.sehnen
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Value("${jwt.tokenHead}")
    String tokenHead;
    @Autowired
    IUserService userService;
    //登录
    @PostMapping("/login")
    public Object login(User user){
        HashMap<String, String> data = new HashMap<>();
        String token = null;
         Long id=  userService.getUserByName(user.getUsername()).getId();
        try {
            token = userService.login(user);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.validateFailed("用户名或密码错误");
        }
        if (StringUtils.isEmpty(token)){
            return CommonResult.validateFailed("用户名或密码错误");
        }

        data.put("tokenHead",tokenHead);
        data.put("access_token",token);
        data.put("id",Long.toString(id));
        // localStorage.setItem("Authorization","Bearer sdsdfdfds")
        // $ajax{data:{},type:"",header:{"Authorization":"Bearer sdsdfdfds"}}
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('/wx/product/index')")
    @GetMapping("/getMessage")
    public CommonResult getMessage(){
        return CommonResult.success(userService.getPermissionsByUserId(2l));
    }
    @PostMapping("/register")
    public CommonResult register(User user){
        Integer res = userService.register(user);
        if(res!=null){
            return  CommonResult.success("注册成功");
        }
        return  CommonResult.failed("注册失败");
    }
    @GetMapping("/list")
    public CommonResult getUserList(User user, Long  pageNum, Long  pageSize,String searchTime) {
        System.out.println(searchTime);
        IPage<User> userModelPageInfo=userService.getUsers(pageNum,pageSize,user,searchTime);
            return  CommonResult.success(userModelPageInfo);

    }
    @PostMapping("/switch/{id}/{status}")
    public CommonResult switchStatus(@PathVariable("id")Long id,@PathVariable("status") Integer status) {
        Integer res=userService.switchStatus(id,status);
        if(res!=null){
            return  CommonResult.success();
        }
        return  CommonResult.failed();
    }
    @PostMapping("/add")
    public  CommonResult  addUser(User user){
        userService.addUser(user);
        return  CommonResult.success();
    }

    @GetMapping("/find/{id}")
    public  CommonResult  findUserById(@PathVariable Long id){
        User user=userService.findUserById(id);
        return  CommonResult.success(user);
    }

    @PostMapping("/delete/{id}")
    public  CommonResult deleteUserById(@PathVariable Long id){
      Integer res=  userService.deleteUserById(id);
      if(res==1){
          return  CommonResult.success();
      }
       return  CommonResult.failed();
    }
//根据用户id获取菜单的选项
    @GetMapping("/menus/{id}")
    public  CommonResult getMenus(@PathVariable Long id){
       List<Permission> permission= userService.getMenusByUserId(id);
        return CommonResult.success(permission);
    }
    @PostMapping("/edit")
    public CommonResult editUser(User user){
        Integer res=userService.editUser(user);
        if(res == 1){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
    @GetMapping("/checkAccessToUri")
    public CommonResult checkAccessToUri(@RequestParam String uri, @RequestParam String username) {
        return userService.checkAccessToUri(uri, username);
    }




}
